import Vue from "vue";
import Router from "vue-router";
import store from "@/store";

Vue.use(Router);

import Layout from "@/layout";
import { Message } from "element-ui";
import getTitle from "@/utils/getTitle";

/**
 * 路由相关属性说明
 * hidden: 当设置hidden为true时，意思不在sideBars侧边栏中显示
 * mete{
 * title: xxx,  设置sideBars侧边栏名称
 * icon: xxx,  设置ideBars侧边栏图标
 * noCache: true  当设置为true时不缓存该路由页面
 * }
 */

/*通用routers*/
export const currencyRoutes = [
  {
    path: "/",
    name: "Home",
    component: Layout,
    redirect: "/application/index",
    children: [
      {
        path: "application/index",
        name: "ApplicationList",
        component: () => import("@/views/application-list"),
        meta: { title: "应用列表", icon: "el-icon-menu" },
      },
    ],
  },
  {
    path: "/cluster-management",
    name: "ClusterManagement",
    component: Layout,
    redirect: "/cluster-management/index",
    children: [
      {
        path: "index",
        name: "ClusterManagement",
        component: () => import("@/views/cluster-management"),
        meta: { title: "集群管理", icon: "el-icon-s-flag" },
      },
    ],
  },
  {
    path: "/personal",
    name: "Personal",
    component: Layout,
    redirect: "/personal/index",
    hidden: true,
    children: [
      {
        path: "index",
        name: "Personal-index",
        component: () => import("@/views/personal"),
        meta: { title: "个人中心" },
      },
    ],
  },
];
/*动态添加routers*/
export const asyncRoutes = [];
const creatRouter = () => {
  return new Router({
    routes: currencyRoutes,
    mode: "history",
    scrollBehavior() {
      return { x: 0, y: 0 };
    },
  });
};

const router = creatRouter();

// 解决addRoute不能删除动态路由问题
export function resetRouter() {
  const reset = creatRouter();
  router.matcher = reset.matcher;
}

// 导航守卫
router.beforeEach(async (to, from, next) => {
  document.title = getTitle(to.meta.title);
  if (to.path === "/") {
    next();
  } else {
    if (store.getters.token) {
      const hasRoles = store.getters.roles.length > 0;
      if (hasRoles) {
        next();
      } else {
        try {
          const { roles } = await store.dispatch("user/_getInfo");
          const addRoutes = await store.dispatch(
            "permission/getAsyncRoutes",
            roles
          );
          router.addRoutes(addRoutes);

          // hack method to ensure that addRoutes is complete
          // set the replace: true, so the navigation will not leave a history record
          next({ ...to, replace: true });
        } catch (error) {
          Message.error(error);
        }
      }
    } else {
      next({
        path: "/login",
        query: {
          redirect: to.fullPath,
        },
      });
    }
  }
});
export default router;
